<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ include file="/common/dateUtil.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<head>
<script>
//////////////////////////////////////////////////////////变量
var adas_lev = {};
adas_lev.debug = true;

adas_lev.data = {};//基本数据
adas_lev.data.alarmBasicDatas = [];//基本报警数据
adas_lev.data.activeSafeName = "主动安全报警";
adas_lev.data.alarmSourceList = [];//主动安全报警列表，默认为空
adas_lev.data.alarmMap = {};//报警缓存map，默认为空
adas_lev.data.alarmTypeList = [];//报警类型列表，默认为空
adas_lev.data.slctPagiUrl = globalConfig.webPath + "/basicSlctAction/basicSelectPaginationData.action";//分页查询地址

adas_lev.cache = {};//缓存对象



//////////////////////////////////////////////////////////加载完成前初始化
//初始化表格
$(function(){
	//数据表格
	$("#adas_lev_Data").datagrid({
		// width:"100%",
		height:"100%",
		toolbar:"#adas_lev_Toobar",//工具条
		pagination:true,
		pageList:[15,30,50,100],
		pageSize:15,
		singleSelect:true,
		rownumbers:true,
		striped:true,
		fitColumns: true,
		checkOnSelect: false,
		selectOnCheck: false,
		onDblClickRow : function(rowIndex,rowData){
			//双击添加查询子页面
			// var param = "?alarmIndex=" + rowData.alarmIndex + "&id=" + rowData.id;
			// var url = "<%=ApplicationPath%>/activeSafeAlarm/oneAdasAlarmInfo.action" + param;
			// addTabToMainTabs(url, newPageTitle);
		}
	});
});

//初始化控件前初始化数据
adas_lev.initDataBeforeInitDivs = function(){
    this.initAlarmBasicData();
}

//初始化基本报警数据
adas_lev.initAlarmBasicData = function(){
    let that = this;
    this.data.alarmBasicDatas = JSON.parse(window.localStorage.getItem("alarmSourceList"));
    // this.outDebug("alarmSourceList : " , this.data.alarmBasicDatas);
    //解析并处理基本数据,只需要保存主动安全相关数据
    that.data.alarmTypeList = [];
    that.data.alarmTypeList.push({text:"全部",value:""});
    if(that.data.alarmBasicDatas.length > 0){
        $.each(that.data.alarmBasicDatas,function(n,item){
            if(item.alarmSourceName == that.data.activeSafeName){
                if(that.data.alarmSourceList.indexOf(item.alarmSource) < 0){
                    that.data.alarmSourceList.push(item.alarmSource);//保存主动安全报警的alarmSource,去重
                }
                let key = item.alarmSource + "_" + item.alarmType;
                that.data.alarmMap[key] = item;//保存报警详细数据至map，key为 “报警来源_报警类型”
                that.data.alarmTypeList.push({text:item.alarmTypeName,value:key});
            }
        });
    }
    // that.outDebug("主动安全报警alarmSourceList ：" , that.data.alarmSourceList);
    // that.outDebug("主动安全报警缓存Map : " , that.data.alarmMap);
    // that.outDebug("主动安全报警类型列表 : " , that.data.alarmTypeList);
}


//////////////////////////////////////////////////////////初始化控件
//接口，初始化控件
adas_lev.initDivs = function(){
    this.initDataBeforeInitDivs();
    this.initDepDiv();
    this.initAlarmLevelDiv();
    this.initAlarmTypeDiv();
    this.initPageChangeDiv();
    this.initTimeDiv();
}

//初始化车组下拉树
adas_lev.initDepDiv = function(){
    //创建下拉部门树
	Utility.createDepTree("adas_lev_depId");
}

//初始化报警级别控件
adas_lev.initAlarmLevelDiv = function(){
    let list = [{text:'全部',value:0},{text:'一级',value:1},{text:'二级',value:2}];
	$("#adas_lev_alarmLevel").combobox({
		textField: "text",
		valueField: "value",
		data: list,
		select: list[0].value
		//选择事件不添加，暂不需要
	});
}

//初始化报警类型控件
adas_lev.initAlarmTypeDiv = function(){
    let that = this;
    $("#adas_lev_alarmType").combobox({
		textField: "text",
		valueField: "value",
		data: that.data.alarmTypeList,
        select: that.data.alarmTypeList[0].value,
        onChange:adas_lev.alarmTypeChange
		//选择事件不添加，暂不需要
	});
}

//初始化页面选择事件
adas_lev.initPageChangeDiv = function(){
    //选择页面事件定义
	$("#adas_lev_Data").datagrid("getPager").pagination({
		onSelectPage:function(pageNumber,pageSize){
            adas_lev.getAllData(pageNumber);
		}
	});
}

//初始化时间控件
adas_lev.initTimeDiv = function(){
    var now = Utility.today();
	$("#adas_lev_startDate").val(now + " 00:00:00");
	$("#adas_lev_endDate").val(now + " 23:59:00");
}

//////////////////////////////////////////////////////////初始化数据
//接口，初始化数据
adas_lev.initDatas = function(){
    this.initAlarmSourListData();
}

//初始化报警信息列表数据
adas_lev.initAlarmSourListData = function(){
    if(this.data.alarmSourceList.length > 0){
        // $("#adas_lev_alarmSourceList").val(this.data.alarmSourceList);
    }
}

//////////////////////////////////////////////////////////查询数据

//获取数据
adas_lev.getAllData = function(pageNumber){
    let that = this;
    let param = this.getSlctParam();
    if(param.parseMsg == ""){
        if(!DataUtil.isDefined(pageNumber) && pageNumber > 0){
            param.pageNumber = pageNumber;
        }
        this.outDebug("开始查询，查询参数：" , param);
        Utility.getAjaxData(adas_lev.data.slctPagiUrl,JSON.stringify(param),function(data){
            that.outDebug("接收到报警分级查询数据：" , data);
            if(data.success){
                $("#adas_lev_Data").datagrid("loadData",data.data);
            }else{
                DataUtil.showErrorMsg(data.message);
            }
        });
    }else{
        DataUtil.showErrorMsg(param.parseMsg);
    }
}

//获取查询参数
adas_lev.getSlctParam = function(){
    let param = {parseMsg:"获取查询相关参数失败！"};
    try {
        let objForm = document.getElementById('adas_lev_queryForm');
        let formData = new FormData(objForm);
        param = DataUtil.parseFormData2Obj(formData);
        // this.outDebug("param = ", param);
        param.pageNumber = $("#adas_lev_Data").datagrid("getPager").data("pagination").options.pageNumber;
        param.pageSize = $("#adas_lev_Data").datagrid("getPager").data("pagination").options.pageSize;
        if(param.alarmLevel == "0"){
            delete param.alarmLevel;
        }
    } catch (error) {
        let errMsg = "获取查询分级报警信息参数失败：" + error;
        param.parseMsg += errMsg;
        DataUtil.outError(errMsg)
    }
    return param;
}

//////////////////////////////////////////////////////////控件事件

//报警类型下拉框选择事件
adas_lev.alarmTypeChange = function(){
    let key = $("#adas_lev_alarmType").combobox("getValue");
    if(key == ""){//未选择值
        $("#adas_lev_alarmSource").val("");
        $("#adas_lev_alarmType_1").val("");
    }else{//选择值
        // adas_lev.outDebug("select key = " + key);
        let obj = adas_lev.data.alarmMap[key];
        if(!DataUtil.isDefined(obj)){
            // adas_lev.outDebug("key--->value = " , obj);
            $("#adas_lev_alarmSource").val(obj.alarmSource);
            $("#adas_lev_alarmType_1").val(obj.alarmType);
        }
    }
}


//格式化报警来源
adas_lev.formatAlarmSource = function(value,rowData,rowIndex){
    let str = "";
    let key = rowData.alarmSource + "_" + rowData.alarmType;
    let obj = adas_lev.data.alarmMap[key];
    if(!DataUtil.isDefined(obj)){
        str = obj.alarmSourceName;
    }
    return str;
}

//格式化报警类型
adas_lev.formatAlarmType = function(value,rowData,rowIndex){
    let str = "";
    let key = rowData.alarmSource + "_" + rowData.alarmType;
    let obj = adas_lev.data.alarmMap[key];
    if(!DataUtil.isDefined(obj)){
        str = obj.alarmTypeName;
    }
    return str;
}

//重置按钮
adas_lev.Reset = function(){
    //初始化车牌号
    $("#adas_lev_plateNo").val("");
    //报警类型
    $("#adas_lev_alarmType").combobox("setValue","");
    //车组
    $("#adas_lev_depId").combotree('setValue', "0");
    //报警级别
    $("#adas_lev_alarmLevel").combobox("setValue","0");
    //时间
    adas_lev.initTimeDiv();
}

//导出按钮点击事件
adas_lev.forExport = function(){
    try {
		$("#adas_lev_queryForm").attr("target", "_blank"); //弹屏下载
		$("#adas_lev_queryForm").submit();
		$("#adas_lev_queryForm").attr("target", "");
	} catch (error) {
		console.log("导出错误："+error);
	}
}


//////////////////////////////////////////////////////////公共方法
//输出调试信息
adas_lev.outDebug = function(msg,data){
    if(this.debug == true){
        DataUtil.outDebug(msg,data);
    }
}



//////////////////////////////////////////////////////////加载后执行
$(document).ready(function(){
    adas_lev.initDivs();//初始化控件
    adas_lev.initDatas();//初始化数据
});
</script>
</head>
<body>
    <!--工具条-->
    <div id="adas_lev_Toobar">
        <form id="adas_lev_queryForm" action="<%=ApplicationPath%>/export.action" class="sh_css_basFontSize">
            <input type="hidden" name="queryId" value="alarmSlct.selectAdasLevelDatas" />
            <input type="hidden" name="fileName" value="报警分级查询信息" />
            <input type="hidden" id="adas_lev_alarmType_1" name="alarmType" value="" />
            <input type="hidden" id="adas_lev_alarmSource" name="alarmSource" value="" />
            <!-- <input type="hidden" id="adas_lev_alarmSourceList" name="alarmSourceList" value="" /> -->
            <table width="100%" class="TableBlock">
                <tr>
                    <td align="right">车牌号码&nbsp;&nbsp;</td>
                    <td>
                        <input type="text" id="adas_lev_plateNo" name="plateNo" style="width:150px;">
                    </td>

                    <td align="right">报警类型&nbsp;&nbsp;</td>
                    <td> <select id="adas_lev_alarmType" style="width:150px;" ></select></td>

                    <td align="right">开始时间
                        :</td>
                    <td>
                        <input type="text" name="startDate" id="adas_lev_startDate" class="datetimepicker" />
                    </td>
                    <td align="right">结束时间:</td>
                    <td align="left">
                        <input type="text" name="endDate" id="adas_lev_endDate" class="datetimepicker" />
                    </td>


                </tr>
                <tr>
                    <td align="right">车组名称&nbsp;&nbsp;</td>
                    <td>
                        <select id="adas_lev_depId" name="depId" style="width:150px;"></select>
                    </td>

                    <td align="right">报警级别&nbsp;&nbsp;</td>
                    <td>
                        <select id="adas_lev_alarmLevel" name="alarmLevel" style="width:150px;">
                            <!-- <option value="1">一级</option>
                            <option value="2">二级</option> -->
                        </select>
                    </td>

                    <td colspan="4" align="left">
                        <div style="width:100%;" class="base_hori_flexStartDiv_css">
                            <div id="adas_lev_btnQuery" class="sh_css_btn" onclick="javascript:adas_lev.getAllData(1);">
                                <div class="ui-icon icon-search" style="width:16px;height:16px;"></div>
                                <span>查询</span>
                            </div>
                            &nbsp;
                            <a id="adas_lev_btnReset" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="adas_lev.Reset();">重置</a>&nbsp;
                            <a id="adas_lev_btnExport" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-excel'" onclick="adas_lev.forExport();"
                                ;">导出</a>
                        </div>
                        <!-- <a id="adas_lev_btnQuery" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="adas_lev.getAllData(1);">查询</a> -->
                        <!--调用utility.js-->
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <!--报警数据表格dagagrid-->
    <table id="adas_lev_Data">
        <thead>
            <tr>
                <th data-options="field:'plateNo'" width="130">车牌号</th>
                <th data-options="field:'depName'" width="130">车组</th>
                <th data-options="field:'alarmType',formatter:adas_lev.formatAlarmType" width="160">报警类型</th>
                <th data-options="field:'alarmSource',formatter:adas_lev.formatAlarmSource" width="160">报警来源</th>
                <th data-options="field:'level'" width="130">报警级别</th>
                <th data-options="field:'location'" width="430">报警地点</th>
                <th data-options="field:'alarmTime'" width="225">报警时间</th>
                <th data-options="field:'speed'" width="100">车速(km/h)</th>
                <th data-options="field:'longitude'" width="130">经度</th>
                <th data-options="field:'latitude'" width="130">纬度</th>
            </tr>
        </thead>
    </table>
</body>